unit main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, ADODB, StdCtrls, ComCtrls, IniFiles, ExtCtrls;

type
  TForm1 = class(TForm)
    ADOConnection1: TADOConnection;
    GroupBox1: TGroupBox;
    Label1: TLabel;
    EdtServer: TEdit;
    StaticText1: TStaticText;
    Label2: TLabel;
    EdtUser: TEdit;
    Label3: TLabel;
    EdtPwd: TEdit;
    Label4: TLabel;
    EdtDbName: TEdit;
    BtUpdate: TButton;
    GroupBox2: TGroupBox;
    lvmemo: TListView;
    ADOComDrop: TADOCommand;
    ADOComAtt: TADOCommand;
    ADOComFinger: TADOCommand;
    ADOComFace: TADOCommand;
    Timer1: TTimer;
    ADOComTemp: TADOCommand;
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure BtUpdateClick(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
    ConnectStr : String;
    procedure InitUpMemo32();  //3.1升级至3.2 内容初始化
    function  GetAdmsPath: String;
    function  GetBataBase:String;
    function  GetPythonStr(ptstr:String):String;
    function  ClearAuthUser:Boolean;
    function  SyncDB:Boolean;
    function  RemoveAttRec:Boolean;
    function  RemoveFingerRec:Boolean;
    function  RemoveFaceRec:Boolean;
  public
    { Public declarations }
    function WaitExeFinish(const sExeName: string):boolean;
  end;

const
  ctable = 'IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N''[dbo].[checkinout]'') AND type in (N''U'')) EXEC sp_rename ''%s'', ''s%''';

var
  Form1: TForm1;


implementation

{$R *.dfm}

function TForm1.WaitExeFinish(const sExeName: string):boolean;
var
    StartupInfo: TStartupInfo;
    ProcessInfo: TProcessInformation;
    b:boolean;
begin
    //ShowMessage('Ready to launch NotePad...');
    FillChar(StartupInfo, SizeOf(StartupInfo), 0);
    b:=CreateProcess( nil, PChar(sExeName), nil, nil, False, 0,
       nil, nil, StartupInfo, ProcessInfo);
    with ProcessInfo do
    begin
       CloseHandle(hThread);
       WaitForSingleObject(hProcess, INFINITE);
       CloseHandle(hProcess);
    end;
    Result := b;
    //ShowMessage('NotePad has terminated.');
end;

function TForm1.ClearAuthUser: Boolean;
begin
   try
      ADOComDrop.Execute;
      Result := True;
   except
      Result := False;
   end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
   InitUpMemo32();
end;

function TForm1.GetAdmsPath: String;
var
  pathStr:String;
begin
    pathStr := ExtractFilePath(Application.ExeName);
    pathStr := StringReplace(pathStr, 'tools', 'units', [rfReplaceAll]);
    Result := pathStr + 'adms\';
end;

function TForm1.GetBataBase: String;
var
    dbfile : String;
    IniFileName: TIniFile;
    dbname, dbserver, uname,upass, constr:String;
begin
    IniFileName := TIniFile.create(GetAdmsPath() + 'attsite.ini');
    if Assigned(IniFileName) then
    begin
       EdtServer.Text := GetPythonStr(IniFileName.ReadString('DATABASE', 'HOST', ''));
       EdtDbName.Text := GetPythonStr(IniFileName.ReadString('DATABASE', 'NAME', ''));
       EdtUser.Text := GetPythonStr(IniFileName.ReadString('DATABASE', 'USER', ''));
       EdtPwd.Text := GetPythonStr(IniFileName.ReadString('DATABASE', 'PASSWORD', ''));
       ConnectStr := Format('Provider=SQLOLEDB.1;Persist Security Info=True;User ID=%s;Password=%s;Data Source=%s;Initial Catalog=%s',[EdtUser.Text, EdtPwd.Text, EdtServer.Text, EdtDbName.Text]);
       Result := ConnectStr;
    end
    else
    begin
      Result := '';
    end;
end;

procedure TForm1.InitUpMemo32;
var
   Item: TListItem;
begin
    with lvmemo do begin
      Items.Clear;
      Items.BeginUpdate;
      item := Items.Add;
      item.Caption := '⊙';
      item.SubItems.Add('清除角色权限');

      item := Items.Add;
      item.Caption := '⊙';
      item.SubItems.Add('同步数据库');

      item := Items.Add;
      item.Caption := '⊙';
      item.SubItems.Add('迁移考勤记录表');

      item := Items.Add;
      item.Caption := '⊙';
      item.SubItems.Add('指纹模板表数据迁移');

      item := Items.Add;
      item.Caption := '⊙';
      item.SubItems.Add('面部模板表数据迁移');

      Items.EndUpdate;
    end;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
   if not FileExists(GetAdmsPath() + 'attsite.ini') then
   begin
      BtUpdate.Enabled := False;
      StaticText1.Caption := '安装目录错误';
      Exit;
   end;
   ConnectStr := GetBataBase();
   Try
      ADOConnection1.ConnectionString := ConnectStr;
      ADOConnection1.Connected := True;
      BtUpdate.Enabled := True;
      StaticText1.Caption := '数据库连接成功';
      Timer1.Enabled := True;
   except
      BtUpdate.Enabled := False;
      StaticText1.Caption := '***数据库连接失败***';
   end;
end;


function TForm1.GetPythonStr(ptstr: String): String;
var
    s:String;
begin
    s := StringReplace(ptstr, 'u"', '', [rfReplaceAll]);
    s := StringReplace(s, '"', '', [rfReplaceAll]);
    s := StringReplace(s, '\\', '\', [rfReplaceAll]);
    Result := s;
end;

procedure TForm1.BtUpdateClick(Sender: TObject);
var
  tt : String;
begin
    StaticText1.Caption := '清除角色权限';
    Application.ProcessMessages;
    if ClearAuthUser() then
    begin
       lvmemo.Items[0].Caption := '√';
       Application.ProcessMessages;
    end
    else
    begin
      StaticText1.Caption := '清除角色权限失败';
      Application.ProcessMessages;
      exit;
    end;

    StaticText1.Caption := '同步数据库';
    Application.ProcessMessages;
    if SyncDB() then
    begin
       lvmemo.Items[1].Caption := '√';
       Application.ProcessMessages;
    end
    else
    begin
      StaticText1.Caption := '同步数据库失败';
      Application.ProcessMessages;
      exit;
    end;  

    StaticText1.Caption := '考勤数据迁移';
    Application.ProcessMessages;
    if RemoveAttRec() then
    begin
         lvmemo.Items[2].Caption := '√';
         Application.ProcessMessages;
{       try
         tt := FormatDateTime('checkinout_YYMMDD', Now);
         ADOComTemp.CommandText := Format(ctable, ['checkinout_old', tt]);
         ADOComTemp.Execute;
         lvmemo.Items[2].Caption := '√';
         Application.ProcessMessages;
       except
          Exit;
       end;   }
    end
    else
    begin
      StaticText1.Caption := '考勤数据迁移失败';
      Application.ProcessMessages;
      exit;
    end;

    StaticText1.Caption := '指纹数据迁移';
    Application.ProcessMessages;
    if RemoveFingerRec() then
    begin
       lvmemo.Items[3].Caption := '√';
       Application.ProcessMessages;
    end
    else
    begin
      StaticText1.Caption := '指纹数据迁移失败';
      Application.ProcessMessages;
      exit;
    end;

    StaticText1.Caption := '面部数据迁移';
    Application.ProcessMessages;
    if RemoveFaceRec() then
    begin
       lvmemo.Items[4].Caption := '√';
       Application.ProcessMessages;
    end
    else
    begin
      StaticText1.Caption := '面部数据迁移失败';
      Application.ProcessMessages;
      exit;
    end;
end;

function TForm1.SyncDB: Boolean;
var
    cc: String;
    StrLst:TStringList;
begin
   if not FileExists(ExtractFilePath(Application.ExeName)+'syncdb.bat') then
   begin
      try
        StrLst := TStringList.Create;
        StrLst.Add('cd ..\units\adms');
        StrLst.Add('python manage.pyc syncdb');
        StrLst.SaveToFile('syncdb.bat');
      finally
        StrLst.Free;
      end;
   end;
   WaitExeFinish('syncdb.bat');
end;

function TForm1.RemoveAttRec: Boolean;
begin
   try
      ADOComAtt.Execute;
      Result := True;
   except
      Result := False;
   end;
end;

function TForm1.RemoveFaceRec: Boolean;
begin
   try
      ADOComFace.Execute;
      Result := True;
   except
      Result := False;
   end;
end;

function TForm1.RemoveFingerRec: Boolean;
begin
   try
      ADOComFinger.Execute;
      Result := True;
   except
      Result := False;
   end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
    Application.ProcessMessages;
end;

end.
